import java.util.Arrays;

public class InsertSort {
    public static void main(String[] args) {
        int array[] = {3,6,2,7,8,5};
        int temp = 0;

        //插入排序法
        //index左边为有序数列，右边为无序数列
        for(int index = 1; index < array.length; index++){
            temp = array[index];
            int leftindex = index - 1;
            //当arra[index] < array[leftindex] 时，交换位置
            //leftindex后的数列依次向后移一位
            while(leftindex >= 0 && temp < array[leftindex]){
                array[leftindex + 1 ] = array[leftindex];
                leftindex--;
            }
            array[leftindex + 1] = temp;
            System.out.printf("第%d轮：",index);
            System.out.println(Arrays.toString(array));
        }
        System.out.println("最终结果：");
        System.out.println(Arrays.toString(array));
    }
}
